﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using Javavaj.iHunter;
using Javavaj.iHunter.Core;

namespace Javavaj.iHunter.Filters
{
    public class KeywordFilter : IFilter
    {
        private XmlDocument settings = null;
        private List<string> whiteList = null;
        private List<string> blackList = null;
        private bool enableWhiteList = true;
        private bool enableBlackList = true;

        public KeywordFilter()
        {
            whiteList = new List<string>();
            blackList = new List<string>();
        }

        /// <summary>
        /// 初始化频道配置
        /// </summary>
        /// <param name="settings">配置文件</param>
        public void InitSettings(XmlDocument sts)
        {
            /* The Xml setting document looks like this:
             * <Settings>
             *     <WhiteList>
             *         <Enable>True</Enable>
             *         <Word>word1</Word>
             *         <Word>word2</Word>
             *         ...
             *     </WhiteList>
             *     <BlackList>
             *         <Enable>False</Enable>
             *         <Word>word1</Word>
             *         <Word>word2</Word>
             *     </BlackList>
             * <Settings>
             */

            whiteList.Clear();
            blackList.Clear();
            settings = sts;

            try
            {
                XmlElement nodeWhite = settings.DocumentElement["WhiteList"];
                XmlElement nodeBlack = settings.DocumentElement["BlackList"];
                
                foreach (XmlElement ele in nodeWhite.GetElementsByTagName("Word"))
                    whiteList.Add(ele.InnerText);
                foreach (XmlElement ele in nodeBlack.GetElementsByTagName("Word"))
                    blackList.Add(ele.InnerText);

                enableWhiteList = bool.Parse(nodeWhite["Enable"].InnerText);
                enableBlackList = bool.Parse(nodeBlack["Enable"].InnerText);
            }
            catch
            {
                //MessageBox.Show("插件配置错误。请重新设置。", "关键字过滤器");
                whiteList.Clear();
                blackList.Clear();
                enableBlackList = false;
                enableWhiteList = false;
            }

            return;
        }

        /// <summary>
        /// 显示频道配置界面
        /// </summary>
        /// <returns></returns>
        public void Config()
        {
            ConfigDialog cd = new ConfigDialog(settings);
            cd.ShowDialog();
            if (cd.DialogResult == DialogResult.OK)
            {
                settings = cd.NewSettings;
                this.InitSettings(settings);
                if (SettingChanged != null)
                    SettingChanged.Invoke(this, new PluginSettingChangedEventArgs(this, settings));
            }
            cd.Close();
        }

        /// <summary>
        /// 某条消息是否可以通过
        /// </summary>
        /// <returns>消息评分</returns>
        public int Rank(IMessage message)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(message.Title); 
            sb.AppendLine(message.Author);
            sb.AppendLine(message.Description);
            if (message.ContentIsBinary == false)
                sb.AppendLine((string)message.Content);

            string str = sb.ToString();

            if (enableWhiteList)
                foreach (string word in whiteList)
                {
                    if (str.Contains(word))
                        return 100;
                }

            if (enableBlackList)
                foreach (string word in blackList)
                {
                    if (str.Contains(word))
                        return 0;
                }

            return 100;
        }

        /// <summary>
        /// 向过滤器提供训练样例(正例/反例)
        /// </summary>
        public void Train(IMessage message, bool label)
        {
            //nothing to do in this filter
        }

        /// <summary>
        /// 频道设置更改
        /// </summary>
        public event PluginSettingChangedEventHandler SettingChanged;
    }
}
